package org.example.server.tcp;

import io.vertx.core.Handler;
import io.vertx.core.Vertx;
import io.vertx.core.buffer.Buffer;
import io.vertx.core.net.NetServer;
import io.vertx.core.parsetools.RecordParser;
import org.example.server.HttpServer;

/**
 * @description TODO
 * @author sats@jz
 * @date 2025年01月03日 19:46
 */
public class VertxTcpServerTest implements HttpServer {
    private byte[] handleRequest(byte[] requestData){
        return "TCP: Hello Client".getBytes();
    }
    @Override
    public void doStart(int port) {
        Vertx vertx = Vertx.vertx();

        // 创建TCP服务器
        NetServer netServer = vertx.createNetServer();
        // 处理请求

        netServer.connectHandler(netSocket -> {

            // 处理连接

//                String testMessage = "Hello server!Hello, Server!Hello server!Hello, Server!";
//                int length = testMessage.getBytes().length;
//
//                // 解决半包和粘包
//                // 构造parse
//                RecordParser recordParser = RecordParser.newFixed(length);
//                recordParser.setOutput(new Handler<Buffer>() {
//                    @Override
//                    public void handle(Buffer buffer) {
//                        String str = new String(buffer.getBytes());
//                        System.out.println(str);
//                        if (testMessage.equals(str)) {
//                            System.out.println("good");
//                        }else{
//                            System.out.println("bad");
//                        }
//                    }
//                });
//                netSocket.handler(recordParser);

//                // 处理接受到的字节数组
//                if(buffer.getBytes().length < length){
//                    System.out.println("半包！" + buffer.getBytes().length);
//                    return;
//                }
//                if (buffer.getBytes().length > length) {
//                    System.out.println("粘包, length = " + buffer.getBytes().length);
//                    return;
//                }
//                String str = new String(buffer.getBytes(0, length));
//                System.out.println(str);
//                if (testMessage.equals(str)) {
//                    System.out.println("good");
//                }



            // 构造parse
            RecordParser recordParser = RecordParser.newFixed(8);
            recordParser.setOutput(new Handler<Buffer>() {
                int size = -1;
                Buffer resultBuffer = Buffer.buffer();
                @Override
                public void handle(Buffer buffer) {
                    if(-1 == size){
                        // 读取消息体长度
                        size = buffer.getInt(4);
                        recordParser.fixedSizeMode(size);
                        resultBuffer.appendBuffer(buffer);
                    }else{
                        resultBuffer.appendBuffer(buffer);
                        byte[] bytes = resultBuffer.getBytes(8, resultBuffer.length());
                        System.out.println(new String(bytes));
                        // 重置一轮
                        recordParser.fixedSizeMode(8);
                        size = -1;
                        resultBuffer = Buffer.buffer();
                    }
                }
            });
            netSocket.handler(recordParser);

        });
//        netServer.connectHandler(new TcpServerHandler());

        netServer.listen(port, netServerAsyncResult -> {
            if (netServerAsyncResult.succeeded()) {
                System.out.println("TCP服务器启动成功，监听端口：" + port);
            } else {
                System.out.println("TCP服务器启动失败：" + netServerAsyncResult.cause());
            }
        });
    }

    public static void main(String[] args) {
        new VertxTcpServerTest().doStart(8088);
    }

}


